//请求开发环境的服务器的axios实例
import axios from 'axios'
//引入nprogress
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
// console.log(process.env,'process.env');

//创建一个实例
const request = axios.create({
  baseURL: process.env.VUE_APP_MOCKAPI,
  timeout: 10000
})
//给实例配置一个拦截器
request.interceptors.request.use(
  (config) => {
    //发送请求触发进度条启动
    nProgress.start()
    return config
  },
  (error) => {

    //请求结束后进度条结束
    nProgress.done()
    return Promise.reject(error)
  }
)

request.interceptors.response.use((response) => {
  //请求结束后进度条结束
  nProgress.done()
  if (response.data.code === 200) {
    return response.data.data
  } else {
    return Promise.reject({ message: response.data.message })
  }
}, (error) => {
  //请求结束后进度条结束
  nProgress.done()
  return Promise.reject(error)
})

export default request
